﻿using HCMS.CommonDtos;
using HCMS.IServices.Base;
using Microsoft.AspNetCore.Mvc;

namespace HCMS.HttpApi.Site.Controllers.Base
{
    /// <summary>
    /// 行政区划 API
    /// </summary>
    public class SystemAreaController : BaseController
    {
        private readonly ISystemAreaAppService systemAreaAppService;

        public SystemAreaController(ISystemAreaAppService systemAreaAppService)
        {
            this.systemAreaAppService = systemAreaAppService;
        }

        /// <summary>
        /// 根据名称获取行政区划代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(AjaxResultDto), StatusCodes.Status200OK)]
        [ProducesResponseType(typeof(AjaxResultDto), StatusCodes.Status500InternalServerError)]
        [Produces("application/json")]
        public async Task<IActionResult> RegionalismCode([FromQuery] string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return Ok(new AjaxResultDto() { Code = 500, Message = "请填写要查找的行政区划名称" });
            }

            var area = await systemAreaAppService.BaseFirstOrDefaultAsync(t => t.Name.Contains(name) && t.Status == 0);
            if (area == null)
            {
                return Ok(new AjaxResultDto() { Code = 500, Message = "没有找到填写的行政区划信息" });
            }

            return Ok(new AjaxResultDto() { Result = area.RegionalismCode });
        }
    }
}
